package com.otp.Activity;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.android.volley.request.JsonObjectRequest;
import com.otp.customUI.NavigationView;
import com.otp.util.Constants;
import com.otp.util.HttpUtil;
import com.otp.util.MyVolley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;


public class UserActivity extends BaseActivity implements View.OnClickListener {

    private TextView back = null;
    private NavigationView navigationView = null;
    private Button loginButton = null;
    private EditText username = null;
    private EditText password = null;
    ProgressDialog dialog = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        back = (TextView) findViewById(R.id.iv_nav_back);
        navigationView = (NavigationView) findViewById(R.id.userTitle);
        navigationView.setTitle("登录");

        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        loginButton = (Button) findViewById(R.id.userLogin);
        loginButton.setOnClickListener(this);
        back.setOnClickListener(this);
    }

    private void userLogin() throws JSONException {
        RequestQueue mRequestQueue = MyVolley.getRequestQueue();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(Constants.USER_NAME, username.getText().toString());
        jsonObject.put(Constants.PASSWORD, password.getText().toString());
        jsonObject.put(Constants.REGISTRATION_ID, this.getRegistrationId());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, HttpUtil.getURL("User/Login?"+Math.random()), jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        try {

                            if (jsonObject.getString(Constants.MESSAGE).equals(Constants.SUCCESS)) {
                                SharedPreferences.Editor shareData = getSharedPreferences(Constants.USER_DATA, 0).edit();
                                shareData.putString(Constants.USER_NAME, username.getText().toString());
                                shareData.putString(Constants.ACCESS_TOKEN, jsonObject.getJSONObject("content").getString(Constants.ACCESS_TOKEN));
                                shareData.commit();
                                UserActivity.this.finish();
                                Toast.makeText(UserActivity.this, "登录成功！", Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }finally {
                            dialogDismiss(dialog);
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        dialogDismiss(dialog);
                        Toast.makeText(UserActivity.this, "登录失败，请重试！", Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Accept", "application/json");
                headers.put("Content-Type", "application/json; charset=UTF-8");
                return headers;
            }
        };
        mRequestQueue.add(jsonObjectRequest);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_nav_back:
                this.finish();
                break;
            case R.id.userLogin:
                try {
                    dialog =  new ProgressDialog(UserActivity.this);
                    dialog.setMessage("正在登录");
                    dialog.setCancelable(false);
                    dialog.show();
                    this.userLogin();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            default:
                ;
                break;
        }
    }

    public void dialogDismiss(ProgressDialog dislog) {
        dislog.dismiss();
    }

    public String getRegistrationId() {
        return JPushInterface.getRegistrationID(this);
    }

    public void finishThisActivity(){
        this.finish();
    }
}
